<template>
  <div style="width: 50%;border: 1px solid;" >
    <div class="card" style="padding: 25px 30px;background: linear-gradient(to bottom, #f5f3f2, #92bdce)">
      <div style="font-size: 20px;color: red;display: flex;align-items: center;justify-content: center">
        <span style="margin-bottom: 20px">个人资料修改</span>
      </div>
      <el-form :model="data.user" ref="formRef" :rules="data.rules" label-width="100px" style="padding-right: 50px">
        <el-form-item label="账号" prop="username">
          <el-input :prefix-icon="User" v-model="data.user.username" autocomplete="off" disabled/>
        </el-form-item>
        <el-form-item label="名称" prop="nickname">
          <el-input :prefix-icon="UserFilled" v-model="data.user.nickname" autocomplete="off" />
        </el-form-item>
<!--        <el-form-item label="所属学院" prop="collegeId">
          <el-input v-model="data.user.collegeId" autocomplete="off" disabled />
        </el-form-item>-->
        <el-form-item label="所属学院" prop="collegeId">
          <el-select style="width: 100%" v-model="data.user.collegeId" disabled>
            <template #prefix>
              <el-icon><School /></el-icon>
            </template>
            <el-option
                v-for="item in data.collegeList"
                :key="item.collegeId"
                :label="item.name"
                :value="item.collegeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select style="width: 100%" v-model="data.user.role" disabled>
            <template #prefix>
              <el-icon><Stamp /></el-icon>
            </template>
            <el-option value="SUPER_ADMIN" label="系统管理员"></el-option>
            <el-option value="ADMIN" label="院管理员"></el-option>
            <el-option value="TEACHER" label="教师管理员"></el-option>
            <el-option value="STUDENT" label="学生"></el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center">
          <el-button style="padding: 10px 25px" type="primary" @click="save">保存个人资料</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref} from "vue"
import {ElMessage} from "element-plus";
import {School, Stamp, User, UserFilled} from "@element-plus/icons-vue";
import alovaInstance from "@/utils/request";


const data = reactive({
  user: JSON.parse(localStorage.getItem('credit-system-user') || '{}'),
  rules: {
    username: [
      {required: true, message: '用户名不能为空', trigger: 'blur'},
    ],
    nickname: [
      {required: true, message: '名称不能为空', trigger: 'blur'},
    ],
    collegeId: [
      {required: true, message: '所属学院不能为空', trigger: 'blur'},
    ],
    role: [
      {required: true, message: '角色不能为空', trigger: 'blur'},
    ],
   },
  collegeList:[]
})

const formRef = ref()
const emit = defineEmits(["updateUser"])

const loadCollegeList = () => {
  alovaInstance.Get('/sysAdminCollege/selectAll').then(res => {
    if (res.code === '200') {
      data.collegeList = res.data
    }
  })
}

// 把当前修改的用户信息存储到后台数据库
const save = () => {
  if (data.user.role === 'ADMIN') {
    formRef.value.validate((valid => {
      if (valid) {
        alovaInstance.Put('/collegeAdmin/update', data.user).then(res => {
          if (res.code === '200') {
            ElMessage.success('更新成功')
            //把更新后的用户信息存储到缓存
            localStorage.setItem('credit-system-user', JSON.stringify(data.user))
            emit('updateUser')
          }
        }).catch(error => {
          console.error('请求失败:', error);
          ElMessage.error(error.message || '网络错误，请稍后重试');
        });
      }
    }))
  }
}
onMounted(()=>{
  loadCollegeList()
})

</script>



<style scoped>
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>